﻿var Course = {
    Register: function () {
        $("span.register_icon24").live("click", function () {
            $("div#loading").css("width", $("div#loading").parent().width());
            $("div#loading").css("height", $("div#loading").parent().height());
            $("div#loading").css("display", "block");
            var courseID = $(this).parent().parent().attr("courseID");
            var obj = $(this);
            $.ajax({
                url: "/Course/Enrol",
                type: "POST",
                data: { "courseID": courseID },
                cache: false,
                dataType: "html",
                success: function (data) {
                    if (data == "") {
                        alert("Bạn đã đăng ký thành công. Hãy chờ chủ khóa học phê duyệt");
                        $(obj).attr("class", "waiting_icon24");
                        $(obj).attr("title", "Đang chờ phê duyệt");
                        $(obj).css("cursor", "default");
                    }
                    else {
                        alert(data);
                    }

                    $("div#loading").css("display", "none");
                }
            });
        });

        $("span.accept_icon24").live("click", function () {
            $("div#loading").css("width", $("div#loading").parent().width());
            $("div#loading").css("height", $("div#loading").parent().height());
            $("div#loading").css("display", "block");
            var enrollmentID = $(this).attr("enrollmentID");
            var obj = $(this);
            $.post("/Course/AcceptOrCancelInvitation", { "enrollmentID": enrollmentID, "isAccept": true }, function (data) {
                if (data == "") {
                    location.reload();
                }
                else {
                    alert(data);
                }

                $("div#loading").css("display", "none");
            });
        });

        $("span.cancel_icon24").live("click", function () {
            $("div#loading").css("width", $("div#loading").parent().width());
            $("div#loading").css("height", $("div#loading").parent().height());
            $("div#loading").css("display", "block");
            var enrollmentID = $(this).attr("enrollmentID");
            var obj = $(this);
            $.post("/Course/AcceptOrCancelInvitation", { "enrollmentID": enrollmentID, "isAccept": false }, function (data) {
                if (data == "") {
                    $(obj).parent().parent().remove();
                }
                else {
                    alert(data);
                }

                $("div#loading").css("display", "none");
            });
        });
    },

    Search: function () {
        $("#txtClassOwner").mousedown(function () {
            if ($(this).val() == "Tên người mở lớp") {
                $(this).css("color", "#000");
                $(this).val("");
            }
        });

        $("#txtClassOwner").blur(function () {
            if ($.trim($(this).val()) == "") {
                $(this).css("color", "#999");
                $(this).val("Tên người mở lớp");
            }
        });
    },

    init: function () {
        Course.Register();
        Course.Search();
    }
}

var CreateCourse = {
    DialogFiles: function () {
        $("span#ChooseExistFiles a.personal_icon36").click(function () {
            $("#dialog").attr("title", "Chọn tài nguyên có sẵn");
            $("#dialog").dialog({
                modal: true,
                width: 550,
                height: 500,
                draggable: true,
                position: 'top',
                buttons: {
                    "OK": function () {
                        $(this).dialog("close");
                    }
                },

                open: function (event, ui) {
                    GetPersonalFiles(1);
                },

                close: function () {
                    $(this).dialog("destroy");
                    $("#dialog").html("<div id=\"dialog-loading\" class=\"loading\"></div>");
                }
            });
        });

        $("span#ChooseExistFiles a.public_icon36").click(function () {
            $("#dialog").attr("title", "Chọn tài nguyên có sẵn");
            $("#dialog").dialog({
                modal: true,
                width: 550,
                height: 500,
                draggable: true,
                position: 'top',
                buttons: {
                    "OK": function () {
                        $(this).dialog("close");
                    }
                },

                open: function (event, ui) {
                    GetCommunityFiles(1);
                },

                close: function () {                    
                    $(this).dialog("destroy");
                    $("#dialog").html("<div id=\"dialog-loading\" class=\"loading\"></div>");
                }
            });
        });
    },

    ChooseFiles: function () {
        $(".tbl1 input:checkbox").click(function () {
            var id = $(this).attr("id");
            if ($(this).is(":checked")) {
                $(this).parent().parent().addClass("active");
                var fileName = $(this).parent().parent().find("td:first").attr("title");
                $("#ChosenFiles").append("<div id='" + id + "'>" + fileName + "</div>");
                $("#ChosenFiles").append("<input type='hidden' name='files' value='" + id + "' />");
                $("#ChosenFiles").append("<div class=\"clearall\"></div>");
            }
            else {
                $(this).parent().parent().removeClass("active");
                $("#ChosenFiles div#" + id).remove();
                $("#ChosenFiles input[value='" + id + "']").remove();
            }
        });
    },

    init: function () {
        CreateCourse.DialogFiles();
        CreateCourse.ChooseFiles();
    }
}

var Schedule = {
    OpenDialog: function () {
        $("tr.scheInfo td span").parent().click(function () {
            var courseID = $("#scheduleTable").attr("courseID");
            var day = $(this).attr("day");
            var month = $(this).attr("month");
            var year = $(this).attr("year");
            $("#dialog").attr("title", day + "/" + month + "/" + year);
            $("#dialog").dialog({
                modal: true,
                width: 550,
                height: 500,
                draggable: true,
                position: 'top',
                buttons: {
                    "OK": function () {                        
                        $(this).dialog("close");
                    }
                },

                open: function (event, ui) {
                    EditSeminarSchedule(courseID, year, month, day);
                },

                close: function () {
                    $(this).dialog("destroy");
                    CheckToDeleteSeminarSchedule(courseID, year, month, day);
                    $("#dialog").html("<div id=\"dialog-loading\" class=\"loading\"></div>");
                }
            });
        });
    },

    GetSeminarSchedule: function () {
        $("#btnGetSeminarSchedule").click(function () {
            var courseID = $("#scheduleTable").attr("courseID");
            var month = $("#scheduleMonth").val();
            var year = $("#scheduleYear").val();
            $("div#scheduleLoading").css("width", $("div#scheduleLoading").parent().width());
            $("div#scheduleLoading").css("height", $("div#scheduleLoading").parent().height());
            $("div#scheduleLoading").css("display", "block");
            $.ajax({
                url: "/Course/Schedule",
                type: "GET",
                data: { "id": courseID, "year": year, "month": month },
                cache: false,
                dataType: "html",
                success: function (data) {
                    $("#ui-tabs-2").html(data);
                    $("div#scheduleLoading").css("display", "none");
                }
            });
        });
    },

    init: function () {
        Schedule.OpenDialog();
        Schedule.GetSeminarSchedule();
    }
}

var Member = {
    ChooseMembers: function () {
        $(".tbl1 input:checkbox").click(function () {
            if ($(this).is(":checked")) {
                $(this).parent().parent().addClass("active");
            }
            else {
                $(this).parent().parent().removeClass("active");
            }
        });
    },

    ExecuteAcceptOrDenyRequest: function () {
        $("span.accept_icon32").click(function () {
            var ids = "";
            $(".tbl1 input:checked").each(function () {
                ids += $(this).attr("enrollmentID") + "|";
            });

            if (ids == "") {
                alert("Vui lòng chọn thành viên để thực hiện thao tác này");
                return;
            }

            $("div.membercontainer div.loading").css("width", $("div.membercontainer div.loading").parent().width());
            $("div.membercontainer div.loading").css("height", $("div.membercontainer div.loading").parent().height());
            $("div.membercontainer div.loading").css("display", "block");

            $.ajax({
                url: "/Course/ChangeEnrollmentStatus",
                type: "POST",
                data: { "enrollmentIDs": ids, "status": 1 },
                cache: false,
                dataType: "html",
                success: function (data) {
                    $("div#ui-tabs-4").html(data);
                    $("div.membercontainer div.loading").css("display", "none");
                }
            });
        });

        $("span.cancel_icon32").click(function () {
            var ids = "";
            $(".tbl1 input:checked").each(function () {
                ids += $(this).attr("enrollmentID") + "|";
            });

            if (ids == "") {
                alert("Vui lòng chọn thành viên để thực hiện thao tác này");
                return;
            }

            $("div.membercontainer div.loading").css("width", $("div.membercontainer div.loading").parent().width());
            $("div.membercontainer div.loading").css("height", $("div.membercontainer div.loading").parent().height());
            $("div.membercontainer div.loading").css("display", "block");

            $.ajax({
                url: "/Course/ChangeEnrollmentStatus",
                type: "POST",
                data: { "enrollmentIDs": ids, "status": 2 },
                cache: false,
                dataType: "html",
                success: function (data) {
                    $("div#ui-tabs-4").html(data);
                    $("div.membercontainer div.loading").css("display", "none");
                }
            });
        });
    },

    DialogInvite: function () {
        $("span.invite_icon32").click(function () {
            $("#dialog").attr("title", "Mời bạn tham gia lớp học");
            $("#dialog").dialog({
                modal: true,
                width: 550,
                height: 500,
                draggable: true,
                position: 'top',
                buttons: {
                    "OK": function () {
                        $(this).dialog("close");
                    }
                },

                open: function (event, ui) {
                    GetFriendToInviteJoinCourse($("#courseID").val());
                },

                close: function () {
                    var accountIDs = "";
                    $(":checked.cbInvite").each(function (index, val) {
                        if (index == 0) {
                            accountIDs = $(this).attr("accountID");
                        }
                        else {
                            accountIDs += "," + $(this).attr("accountID");
                        }
                    });

                    $("div#dialog-loading").css("display", "block");
                    $.ajax({
                        url: "/Course/Invite",
                        type: "POST",
                        data: { "courseID": $("#courseID").val(), "accountIDs": accountIDs },
                        cache: false,
                        dataType: "html",
                        success: function (data) {
                            $(this).dialog("destroy");
                            $("#dialog").html("<div id=\"dialog-loading\" class=\"loading\"></div>");
                        }
                    });
                }
            });
        });
    },

    init: function () {
        Member.ChooseMembers();
        Member.ExecuteAcceptOrDenyRequest();
        Member.DialogInvite();
    }
}

var Resource = {
    OnDelete: function () {
        $("span.delete_icon16").click(function () {
            if (confirm("Bạn muốn xóa tài nguyên này ?") == true) {
                var deletedRow = $(this).parent().parent();
                $(deletedRow).fadeOut('slow', function () {
                    $(deletedRow).remove();
                });
            }
        });
    },

    OnSave: function () {
        $("input:button").click(function () {        
            var form = $("form").serialize();
            $("div#resourceLoading").css("width", $("div#resourceLoading").parent().width());
            $("div#resourceLoading").css("height", $("div#resourceLoading").parent().height());
            $("div#resourceLoading").css("display", "block");
            $.post("/Course/Resources", form, function (returnHtml) {
                $("div#ui-tabs-3").html(returnHtml);
                $("div#resourceLoading").css("display", "none");
            });
        });
    },

    init: function () {
        Resource.OnDelete();
        Resource.OnSave();
    }
}

function GetPersonalFiles(page) {
    $("div#dialog-loading").css("width", $("div#dialog-loading").parent().width());
    $("div#dialog-loading").css("height", $("div#dialog-loading").parent().height());
    $("div#dialog-loading").css("display", "block");
    $.ajax({
        url: "/Course/GetPersonalFilesToChoose",
        type: "GET",
        data: { "page": page },
        cache: false,
        dataType: "html",
        success: function (data) {
            $("#dialog").html(data);
            $(".tbl1 input:checkbox").each(function () {
                var id = $(this).attr("id");
                if($("input:hidden[value='" + id + "']").length > 0) {
                //if ($("#ChosenFiles div#" + id).length > 0) {
                    $(this).attr("checked", "checked");
                    $(this).parent().parent().addClass("active");
                }
            });

            $("ul.pager li a").click(function () {
                GetPersonalFiles($(this).attr("page"));
            });

            CreateCourse.ChooseFiles();
            $("div#dialog-loading").css("display", "none");
        }
    });
}

function GetCommunityFiles(page) {
    $("div#dialog-loading").css("width", $("div#dialog-loading").parent().width());
    $("div#dialog-loading").css("height", $("div#dialog-loading").parent().height());
    $("div#dialog-loading").css("display", "block");
    $.ajax({
        url: "/Course/GetCommunityFilesToChoose",
        type: "GET",
        data: { "page": page },
        cache: false,
        dataType: "html",
        success: function (data) {
            $("#dialog").html(data);
            $(".tbl1 input:checkbox").each(function () {
                var id = $(this).attr("id");
                if($("input:hidden[value='" + id + "']").length > 0) {
                //if ($("#ChosenFiles div#" + id).length > 0) {
                    $(this).attr("checked", "checked");
                    $(this).parent().parent().addClass("active");
                }
            });

            $("ul.pager li a").click(function () {
                GetCommunityFiles($(this).attr("page"));
            });

            CreateCourse.ChooseFiles();
            $("div#dialog-loading").css("display", "none");
        }
    });
}

function EditSeminarSchedule(id, year, month, day) {
    $("div#dialog-loading").css("width", $("div#dialog-loading").parent().width());
    $("div#dialog-loading").css("height", $("div#dialog-loading").parent().height());
    $("div#dialog-loading").css("display", "block");
    $.ajax({
        url: "/Course/EditSeminarSchedule",
        type: "GET",
        data: { "id": id, "year": year, "month": month, "day": day },
        cache: false,
        dataType: "html",
        success: function (data) {            
            $("#dialog").html(data);
            $("div#dialog-loading").css("display", "none");
        }
    });
}

function CheckToDeleteSeminarSchedule(id, year, month, day) {
    $("div#scheduleLoading").css("width", $("div#scheduleLoading").parent().width());
    $("div#scheduleLoading").css("height", $("div#scheduleLoading").parent().height());
    $("div#scheduleLoading").css("display", "block");
    $.ajax({
        url: "/Course/ReloadSeminarScheduleAfterEdit",
        type: "GET",
        data: { "id": id, "year": year, "month": month, "day": day },
        cache: false,
        dataType: "html",
        success: function (data) {
            $("#ui-tabs-2").html(data);
            $("div#scheduleLoading").css("display", "none");
        }
    });
}

function GetFriendToInviteJoinCourse(courseID) {
    $("div#dialog-loading").css("width", $("div#dialog-loading").parent().width());
    $("div#dialog-loading").css("height", $("div#dialog-loading").parent().height());
    $("div#dialog-loading").css("display", "block");
    $.ajax({
        url: "/Course/Invite",
        type: "GET",
        data: { "courseID": courseID },
        cache: false,
        dataType: "html",
        success: function (data) {
            $("#dialog").html(data);   
                                
            $("div#dialog-loading").css("display", "none");
        }
    });
}